package com.macro.mall.portal.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * class
 *
 * @author xuyanjun
 * @date 2019-06-29
 */
public class WebUtils {

    private static final String X_REAL_IP = "X-Real-IP";
    private static final String X_FORWARDED_FOR = "X-Forwarded-For";
    private static final String X_FORWARDED_FOR_SPLIT_SYMBOL = ",";

    public static final String CLIENT_APPNAME_HEADER = "Client-AppName";

    public static final String USER_AGENT_HEADER = "User-Agent";

    /**
     * 获取当前httpRequest
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getRequest();
        }
        return null;
    }

    /**
     * 获取当前httpResponse
     *
     * @return
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getResponse();
        }
        return null;
    }

    /**
     * get real client ip
     * <p>
     * first use X-Forwarded-For header    https://zh.wikipedia.org/wiki/X-Forwarded-For
     * next nginx X-Real-IP
     * last {@link HttpServletRequest#getRemoteAddr()}
     *
     * @return
     */
    public static String getRemoteIp() {
        HttpServletRequest request = getRequest();

        String xForwardedFor = request.getHeader(X_FORWARDED_FOR);
        if (!StringUtils.isBlank(xForwardedFor)) {
            return xForwardedFor.split(X_FORWARDED_FOR_SPLIT_SYMBOL)[0].trim();
        }
        String nginxHeader = request.getHeader(X_REAL_IP);
        return StringUtils.isBlank(nginxHeader) ? request.getRemoteAddr() : nginxHeader;
    }

    /**
     * 获取请求域名
     *
     * @return 请求域名
     */
    public static String getRequestDomain() {
        HttpServletRequest request = getRequest();
        StringBuffer url = request.getRequestURL();
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getSession().getServletContext().getContextPath()).append("/").toString();
    }

    /**
     * 获取用户代理
     *
     * @return 用户代理
     */
    public static String getUserAgent() {
        HttpServletRequest request = getRequest();
        return request.getHeader(USER_AGENT_HEADER);
    }

    /**
     * 用户是否使用ios
     *
     * @return isUserAgentIOS
     */
    public static boolean isUserAgentIos() {
        return getUserAgent().toLowerCase().contains("iphone");
    }

    /**
     * 获取当前用户
     *
     * @return 当前用户
     */
    public static UserDetails getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (UserDetails) authentication.getPrincipal();
    }
}
